package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

/**
 * Created by Binsabbar on 02/10/2014.
 *
 */
public class BibliotecaLoader {

  private final String TEXT_EXTENSION = "text";

  protected boolean validateFileExtension(String fileName){
    if(fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0) {
      String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1);
      return fileExtension.equals(TEXT_EXTENSION);
    }
    return false;
  }

  protected BufferedReader getFile(String pathToFile, String fileName) throws FileNotFoundException {
    if (!validateFileExtension(fileName)) return null;

    File dataFile = new File(pathToFile, fileName);
    if (dataFile.exists() && dataFile.canRead()){
      return new BufferedReader(new FileReader(dataFile));
    }
    return null;
  }

}
